﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AiFrame.InterfaceLib;
using AiFrame.InterfaceLib.Data.Patterns;
using AiFrame.InterfaceLib.MVP;

namespace AiFrame.Interface.WinUI.Windows
{
    /// <summary>
    /// A pre-defined window manager for a TabControl.
    /// </summary>
    public abstract class TabControlSubWindowContainer<T>: ISubWindowContainer<T>
    {
        private TabControlWindowManager<T> _windowManager;

        private TabControl _tabControl;

        private IDataSourceProvider<T> _dataSourceProvider;

        public TabControlSubWindowContainer(TabControl tabControl, IDataSourceProvider<T> dataSourceProvider)
        {
            _tabControl = tabControl;
            _dataSourceProvider = dataSourceProvider;
            _windowManager = new TabControlWindowManager<T>(_tabControl, dataSourceProvider);
        }

        public bool DataChanged { get; private set; }

        public abstract string ID { get; }

        public IWindowManager<ISaveableWindow<T>> GetWindowManager()
        {
            return _windowManager;
        }

        public void CloseSubWindows()
        {
            _windowManager.CloseAllWindows();
        }

        public IDataSourceProvider<T> DataSourceProvider
        {
            get { return _dataSourceProvider; }
        }
    }

    class TabControlWindowManager<T>: IWindowManager<ISaveableWindow<T>>
    {
        private TabControl _parentControl;

        /// <summary>
        /// The window collection.
        /// </summary>
        private IObservableList<ISaveableWindow<T>> _wndCollection;

        private IDataSourceProvider<T> _dataSourceProvider;

        public TabControlWindowManager(TabControl parentControl, IDataSourceProvider<T> dataSourceProvider)
        {
            _parentControl = parentControl;
            _dataSourceProvider = dataSourceProvider;
            _wndCollection = new ObservableList<ISaveableWindow<T>>();
            _wndCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_wndCollection_CollectionChanged);
        }

        void _wndCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _parentControl.TabPages.Clear();
            foreach (var controllableDataWindow in _wndCollection)
            {
                TabPage tabPage = new TabPage(controllableDataWindow.GetCaption());
                controllableDataWindow.SetParentWindow(tabPage);
                _parentControl.TabPages.Add(tabPage);
            }
        }

        public void RefreshView()
        {
            // Refresh actions
        }

        public void RegisterWindow(ISaveableWindow<T> window)
        {
            if (_parentControl == null)
                throw new MissingInitialisationException("Der WindowManager muss zuerst initialisiert werden.");

            _wndCollection.Add(window);
            window.SetDataSource(_dataSourceProvider.GetDataSource());
        }

        public void CloseWindow(ISaveableWindow<T> window)
        {
            throw new NotImplementedException();
        }

        public void CloseAllWindows()
        {
            _wndCollection.Clear();
        }

        public bool ExistsWindow<W>()
        {
            throw new NotImplementedException();
        }

        public void BringWindowToFront<W>()
        {
            throw new NotImplementedException();
        }

        public ISaveableWindow<T> ActiveWindow
        {
            get { throw new NotImplementedException(); }
        }

        public event EventHandler<ActiveWindowChangedParams<ISaveableWindow<T>>> ActiveWindowChanged;
        public event EventHandler<ActiveWindowChangedParams<ISaveableWindow<T>>> ActiveWindowChanges;
    }
}
